import processing.core.*;
import processing.xml.*;

import processing.opengl.*;
import codeanticode.glgraphics.*;
import deadpixel.keystone.*;

import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;

public class CornerPinOpenGL extends OpenNiEMWrapper {

	/**
	 * This is another example of a keystoned surface, this time using Andres
	 * Colubri's GLGraphics library. The library has an offscreen renderer that
	 * will draw your sketch to an OpenGL texture.
	 * 
	 * To run this example, you'll need the GLGraphics library. Get it here:
	 * http://users.design.ucla.edu/~acolubri/processing/glgraphics/home/
	 */

	// this object is key! you can use it to render fully accelerated
	// OpenGL scenes directly to a texture
	GLGraphicsOffScreen offscreen1;

	Keystone ks1;
	CornerPinSurface surface1;

	public void setup() {
		size(1024, 768, GLConstants.GLGRAPHICS);
		super.setup();
		offscreen1 = new GLGraphicsOffScreen(this, width, height);

		ks1 = new Keystone(this);
		surface1 = ks1.createCornerPinSurface(width, height, 20);
	}

	public void draw() {
		// convert
		background(0);
		super.update();

		PVector mouse = surface1.getTransformedMouse();

		// first draw the sketch offscreen
		offscreen1.beginDraw();
		offscreen1.background(50);
		offscreen1.lights();
		offscreen1.fill(255);
		offscreen1.translate(mouse.x, mouse.y);
		offscreen1.rotateX(millis() / 200.0f);
		offscreen1.rotateY(millis() / 400.0f);
		offscreen1.box(100);
		offscreen1.endDraw();

		// then render the sketch using the
		// keystoned surface
		if (!calibrating)
			surface1.render(offscreen1.getTexture());
	}

	// Controls for the Keystone object
//	public void keyPressed() {
//
//		switch (key) {
//		case 'c':
//			// enter/leave calibration mode, where surfaces can be warped
//			// & moved
//			ks1.toggleCalibration();
//			break;
//
//		case 'l':
//			// loads the saved layout
//			ks1.load();
//			break;
//
//		case 's':
//			// saves the layout
//			ks1.save();
//			break;
//		}
//	}

	static public void main(String args[]) {
		PApplet.main(new String[] { "--bgcolor=#F0F0F0", "CornerPinOpenGL" });
	}
}
